package desenhando;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

import dados.Curva;
import dados.Ponto;

public class EstadoDesenhando {

	private ArrayList<Curva> curvas;
	private int curveIndex;

	private AtomicBoolean animatedPolygonals;
	private boolean animatedCurves;
	
	private int nextColor;

	public EstadoDesenhando() {
		this.curvas = new ArrayList<Curva>();
		this.curvas.add(new Curva(Color.RED));
		this.curveIndex = 0;
		
		this.animatedPolygonals = new AtomicBoolean(false);
		this.animatedCurves = false;
		
		this.nextColor = 0;
	}

	public void addPoint(Ponto p) {
		this.curvas.get(curveIndex).add(p);
	}

	public void addCurve(Curva c) {
		this.curvas.add(c);
	}
	
	public void drawAllControlPoints(Graphics g) {

		for (int i = 0; i < this.curvas.size(); i++) {
			this.curvas.get(i).drawControlPoints(g);
		}
	}

	public void drawAllMainControlPolygonals(Graphics g) {

		Ponto a, b;
		for (int i = 0; i < this.curvas.size(); i++) {
			this.curvas.get(i).drawAllMainControlPolygonals(g);
			
		}
	}

	public void drawAllControlPolygonals(Graphics g, double t) {
		// TODO verify which polygonal must be drawn

		for (int i = 0; i < this.curvas.size(); i++) {
			this.curvas.get(i).drawControlPolygonals(t, g);
		}

	}

	public void drawAllCurves(Graphics g) {
		// TODO verify which curve must be drawn
		for (int i = 0; i < this.curvas.size(); i++) {
			this.curvas.get(i).drawCurve(g);
			
			
			
			
		}

	}

	public synchronized void drawSecondaryPolygonals(Graphics g) {
		for (int i = 0; i < this.curvas.size(); i++) {
			Curva c = this.curvas.get(i);
			c.drawControlPolygonals(c.gettAnimation(), g);
		}

	}
	
	public void pointSelection(Graphics g, Ponto p){
		Operacoes.drawPoint(p, g);
	}

	public void setNDivisions(int curveIndex, int nDivisions){
		this.curvas.get(curveIndex).setN_divisions(nDivisions);
	}
	
	// GETTERS AND SETTERS
	public int getCurveIndex() {
		return curveIndex;
	}

	public void setCurveIndex(int curveIndex) {
		this.curveIndex = curveIndex;
	}

	public ArrayList<Curva> getCurves() {
		return curvas;
	}

	public void setCurves(ArrayList<Curva> curves) {
		this.curvas = curves;
	}

	public boolean isAnimatedPolygonals() {
		return animatedPolygonals.get();
	}

	public void setAnimatedPolygonals(boolean animatedPolygonals) {
		this.animatedPolygonals.getAndSet(animatedPolygonals);
		for (int j = 0; j < this.getCurves().size(); j++) {
			this.getCurves().get(j).setAnimated(animatedPolygonals);	
		}
	}

	public boolean isAnimatedCurve() {
		// TODO Auto-generated method stub
		return this.animatedCurves;
	}

	public int getNextColor() {
		return nextColor;
	}

	public void setNextColor(int nextColor) {
		this.nextColor = nextColor;
	}

	public void incrementColor() {
		this.nextColor++;
	}
	
}
